
//url路径
// var URL = getInitUrls();
//公共顶部悬浮导航栏搜索点击下拉提示列表
homeTopInp.focus(function(){
    if($('#xiala li').html() != undefined){
        $(this).siblings('.search-tips-bannner').show();
    }
});


//左边栏出现以及锚点active的切换及跳转位置调整
var asideLeft = $('.aside-left');
var asideLeftAs = $('.aside-left>a');
var moduleTits = $('.bt-module>.module-tit');
var eleBgModuleBottom = $('#element').offset().top+$('#element').height()/2+200;
var moduleTitsOffsetTop = [];
var asideLeftAsOldAct = null;
asideLeft.display = false;
moduleTits.each(function(i,ele){
    var ele = $(ele);
    if(i == 0) return;
    moduleTitsOffsetTop.push(ele.offset().top-100);
});
moduleTitsOffsetTop.push(eleBgModuleBottom);
$(document).scroll(function(){
    var $scrollTop = $(this).scrollTop();
    // scrollTop大于一屏的高度并且还没到底部时左边栏存在
    if($scrollTop > moduleTitsOffsetTop[0] && $scrollTop < eleBgModuleBottom) {
        for (var i = 0;i < 5;i++) {
            if ($scrollTop >= moduleTitsOffsetTop[i] && $scrollTop < moduleTitsOffsetTop[i+1]) {
                if (asideLeftAsOldAct === i) return;
                asideLeftAs.eq(asideLeftAsOldAct).removeClass('active');
                asideLeftAs.eq(i).addClass('active');
                asideLeftAsOldAct = i;
            }
        }
        if (asideLeft.display) return;
        asideLeft.css({'pointer-events': 'auto','opacity':1});
        asideLeft.display = true;            
    }else {
        if (asideLeft.display) {
            asideLeft.css({'pointer-events': 'none', 'opacity':0}); 
            asideLeft.display = false;
            asideLeftAsOldAct = 7
        }
    }
});  
asideLeftAs.click(function(){
    var $scrollTo = ($($(this).attr('href'))).offset().top-50;
    var currentTop = $('body').scrollTop();
    var scrollTime = Math.abs($scrollTo-currentTop)/qWindowHeight*200;
    $('html,body').animate({
        scrollTop: $scrollTo
    }, scrollTime);
    return false;
});
function asideLeftSwitch(){

}
//大轮播图
var $casBox = $('.slide-home');

initialCas($casBox);
var timerCas;
function initialCas($casBox){
    var next = $casBox.find('.next');
    var prev = $casBox.find('.prev');
    var ul = $casBox.find('.slide-wrap');
    var focuses = $('.slide-focus>li');
    var liWidth = ul.find('li').width();
    var count = ul.find('li').size();
    var current = 1;
    var oldFocus = 1;
    ul.css('width',liWidth*count);
    focuses.eq(current-1).addClass('active');
    window.onresize = function(){
        ul.css({
            left: ''
        });
        current = 1;
        focuses.eq(oldFocus-1).removeClass('active');
        focuses.eq(current-1).addClass('active');
        liWidth = ul.find('li').width();
    }
    next.click(function() {
        carousel(ul,true,liWidth,count);
    });
    prev.click(function() {
        carousel(ul,false,liWidth,count);
    });
    clearInterval(timerCas);
    timerCas = setInterval(function() {
        carousel(ul,true,liWidth,count);
    }, 3000);
    $casBox.find('>i').hover(function() {
        clearInterval(timerCas);
    }, function() {
        clearInterval(timerCas);
        timerCas = setInterval(function() {
            carousel(ul,true,liWidth,count);
        }, 3000);
    });
    window.onblur = function(){
        clearInterval(timerCas);
    }
    window.onfocus = function(){
        // window.location.reload();
        clearInterval(timerCas);
        timerCas = setInterval(function() {
            carousel(ul,true,liWidth,count);
        }, 3000);
    }
    //focus点击事件
    focuses.each(function(i,ele){
        var index = i+1; 
        $(ele).hover(function(){
            clearInterval(timerCas);
        },function(){
            clearInterval(timerCas);
            timerCas = setInterval(function() {
                carousel(ul,true,liWidth,count);
            }, 3000);
        });

        $(ele).click(function(){
            var isNext = true;
            // var imgCount = count;
            if (index < current) {
                isNext = false;
            }else if (index == current) return;
            current = index;
            scrollFn(ul,isNext,liWidth,count);
        });
    });
    function carousel($ele,isNext,liWidth,imgCount) {
        //参数为true为下一张,false为上一张
        isNext ? current++ : current--; 
        scrollFn($ele,isNext,liWidth,imgCount);
    };
    function scrollFn($ele,isNext,liWidth,imgCount){
        var $left = -current*liWidth;
        var currentFo = focuses.eq(current-1);
        var oldFo = focuses.eq(oldFocus-1);
        // oldFo.removeClass('active');
        focuses.each(function(i,ele){
            $(ele).removeClass('active');
        });
        currentFo.addClass('active');
        currentFo.css('border',0);
        cTween($ele.get(0),{
            left: {
                target: $left,
                duration: 400,
                fx: 'linear'
            }
        },function(){
            if(isNext) {
                // 滑动到倒数第二张，拉回第一张 
                if(current >= imgCount-2) {
                    ul.css('left',0);
                    current = 0;
                }
            }else {
                // 在第一张的时候点击上一张，瞬间拉到最后一张
                if(current <= 0) {
                    ul.css('left',-liWidth*(imgCount-2));
                    current = imgCount-2;
                }
            }
            oldFo.css('border','1px solid rgba(0, 0, 0, .2)');
            oldFocus = current;      
        });
    }
}



//小轮播图
(function(){
    var $swiperWrap = $('.swiper-box');
    var liWidth = $swiperWrap.find('>div').width()+102;
    var count = $swiperWrap.find('>div').size();
    var times = 0;
    var timer = setInterval(function() {
        carousel($swiperWrap,true,liWidth,count);
    }, 3000);
    function carousel($ele,isNext,liWidth,imgCount) {
        //参数为true为下一张,false为上一张
        var left = parseInt($ele.css('left'));
        $left = isNext ? left-liWidth : left+liWidth;
        $ele.stop(false,true);
        $ele.animate({
            left: $left
        }, 400 , function($ele){
            times++;
            if(times ==  11) {
                $swiperWrap.css('left',0);
                times = 0;
            }
        });
    };
})()

//友情链接active
var yqLinks = $('.bt-body .link-box nav:first-child a');
yqLinks.eq(0).addClass('active');
yqLinks.hover(function(){
    $(this).addClass('active').siblings().removeClass('active');
});

    //搜索预加载
    // $(document).ready(function(){
        // $.ajax({
            // type: 'POST',
            // url: '/index.php?m=Ajaxsearch&a=def',
            // data: {id: 1},
            // dataType: 'json',
            // success: function (rs) {
                // $('#history').html('');
                // $('#xiala').html('');
                // var html = '<span>搜索历史 :</span>';
                // var html2 = '';
                // var num = 1;
                // for (var i in rs.new) {
                    // if(rs.new[i] != null){
                        // html += '<a href="javascript:;" onclick="on_search('+ "'"+ rs.new[i].name + "'" +',this)">'+ rs.new[i].name + '</a>';
                        // $('#history').html(html);
                        // num++;
                    // }
                // }
                // var l = 1;
                // for (var j in rs.newTop) {
                    // if (l <= 3) {
                        // html2 += '<li class="z_spanName">';
                        // html2 +=    '<a id="u_'+num+'" onclick="on_search('+ "'"+rs.newTop[j].name + "'" +',this)" href="javascript:;" class="every-record">';
                        // html2 +=       '<span class="record-name">' + rs.newTop[j].name + '</span>';
                        // html2 +=       '<span class="record-number-wrap"><span class="record-number">' + rs.newTop[j].count + '</span>结果</span>';
                        // html2 +=    '</a>';
                        // html2 +='</li>';
                        // num++;
                    // } else {
                        // html2 += '<li class="z_spanName">';
                        // html2 +=    '<a id="u_'+num+'" onclick="on_search('+ "'"+rs.newTop[j].name + "'" +',this)" href="javascript:;" class="every-record">';
                        // html2 +=       '<span class="record-name">' + rs.newTop[j].name + '</span>';
                        // html2 +=       '<span class="record-number-wrap"><span class="record-number">' + rs.newTop[j].count + '</span>结果</span>';
                        // html2 +=    '</a>';
                        // html2 +='</li>';
                    // }
                    // num++;
                    // l++
                    // $('#xiala').html(html2);
                // }
            // }
        // });
    // });

    /**
     * 下拉点击搜索
     */
    function on_search(keyword,e,isHistory){
        //搜索事件埋点
        var isHistory = isHistory ? true : false;
        var isRecommend = isHistory ? false : true;
        //搜索数据埋点
        $.getJSON('http://api.ibaotu.com/?m=search&a=getSearchResultCount&callback=?',{k:keyword},function(res){
            var resultCount = Number(res);
            sa.track('search', {
                keyWord: keyword,
                resultCount: resultCount,
                isHistory: isHistory,
                isRecommend: isRecommend,
                searchPosition: '首页搜索框'
            });
        });
        $('.home .search-tips-bannner').hide();
        $('#so-kw').val(keyword)
        var sort =  $(e).attr('id')
        $('#sort').val(sort)
        search(keyword)
    }

    /**
     * 搜索JS
     */
    var formSearch = function(){
        var kw = $.trim($('#so-kw').val());
        if(kw == '' || kw == '搜索素材'){
            alert('请填写关键词');
            return false;
        }
		location.href='/plus/search.php?q='+kw;
    }
    $('.but-search').on('click', formSearch);
    $('#so-kw').keydown(function(event){if(event.which == 13){formSearch();}});

    function search(kw){
        $.get("/index.php?m=search&a=check", {kw:kw}, function(data) {
            if(data.status == 1){
                tj_keyword()
                setTimeout(function(){
                    if(data.rewrite){
                        window.location.href="/index.php?m=tags&a=index&class=0-0-0-0-0-1&key=" + data.py;
                    }else{
                        window.location.href=URL.DOMAIN+"/tupian/"+data.py+".html";
                    }
                },500);
            }else{
                alert('请填写有效的关键词');
            }
        }, 'json');
    }


    /**
     * 搜索
     */
    $("#so-kw").keyup(function(H){
        if (window.event) {
            var G = window.event.keyCode;
        } else {
            var G = H.which;
        }
        if (G != 38 && G != 40 && G != 13) {
            var num = 1;
            D = 0;
            if($('#so-kw').val() == ''){
                //默认搜索词
            }else{
                $('#sort').remove()
                $('.search-history').remove();
                $('.recommend-wrap').css("padding-top","0");
                $.ajax({
                    type: 'POST',
                    url: '/index.php?m=Ajaxsearch&a=index',
                    data: {keyword: $('#so-kw').val()},
                    dataType: 'json',
                    success: function (rs) {
                        $('#history').html('');
                        $('#xiala').html('');
                        var html = '';
                        for (var i in rs) {
                            html += '<li class="z_spanName">';
                            html +=    '<a id="u_'+num+'" onclick="on_search('+ "'"+rs[i].keyword + "'" +',this)" href="javascript:;" class="every-record">';
                            html +=       '<span class="record-name">' + rs[i].keyword + '</span>';
                            html +=       '<span class="record-number-wrap"><span class="record-number">' + rs[i].icount + '</span>结果</span>';
                            html +=    '</a>';
                            html += '</li>';
                            $('#xiala').html(html);
                            num++
                        }
                       if(num <= 1){
                            $('.search-tips-bannner').hide();
                       }else{
                           $('.search-tips-bannner').show();
                       }
                    }

                });
            }
        } else {
            topkey(H)
        }
    });

    /**
     * 联想搜索
     */
    var D = 0;
    function topkey(H) {
        if (window.event) {
            var G = window.event.keyCode;
        } else {
            var G = H.which;
        }
        if (G != 38 && G != 40 && G != 13) {
            D = 0;
        } else {
            if ($(".search-tips-bannner").css("display") == "block") {
                var I = $(".z_spanName").length;
                if (G == 38) {
                    D--;
                    if (D < 1) {
                        D = I;
                        C(D);
                        F(1);
                    } else {
                        F(D + 1);
                        C(D);
                    }
                    return false;
                }
                if (G == 40) {
                    D++;
                    if (D > I) {
                        D = 1;
                        C(1);
                        F(I);
                    } else {
                        F(D - 1);
                        C(D);
                    }
                    return false;
                }
            } else {
                D = 0;
            }
        }
    }
    function C(G) {
        $("#u_" + G).parent().css("background-color", "#EBEBEB");
        var name = $("#u_" + G + " .record-name").html()
        $('#so-kw').val(name)
    }
    function F(G) {
        $("#u_" + G).parent().css("background-color", "#fff");
    }
